﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MacomberMapSystem.Common.User_Interfaces.ModelSampler
{
    /// <summary>
    /// This class holds information on a sampler linkage
    /// </summary>
    public class clsSamplerLinkage : ListViewItem
    {
        #region Variable declarations
        /// <summary>The full type name of our variable</summary>
        public String TypeName;

        /// <summary>The collection of incoming links we should parse</summary>
        public clsSamplerLinkage[] IncomingLinks = new clsSamplerLinkage[0];

        /// <summary>The collection of outgoing links we should parse</summary>
        public clsSamplerLinkage[] OutgoingLinks = new clsSamplerLinkage[0];
        #endregion

        #region Initialization
        /// <summary>
        /// Intiailize a new type linkage
        /// </summary>
        /// <param name="TypeName"></param>
        private clsSamplerLinkage(string TypeName)
        {
            this.TypeName = TypeName;
            this.Text = TypeName.Substring(TypeName.IndexOf(':') + 1);
            this.SubItems.Add("0");
            this.SubItems.Add("0");
        }

        /// <summary>
        /// Add in a linkage
        /// </summary>
        /// <param name="TypeName"></param>
        /// <param name="Linkages"></param>
        public static void AddLinkage(String TypeName, SortedDictionary<String, clsSamplerLinkage> Linkages)
        {
            clsSamplerLinkage NewLink = new clsSamplerLinkage(TypeName);
            Linkages.Add(NewLink.Text, NewLink);
        }
        #endregion

        #region Linkage updating
        /// <summary>
        /// Add an incoming link
        /// </summary>
        /// <param name="Link"></param>
        /// <param name="Propigate"></param>
        public void AddIncomingLink(clsSamplerLinkage Link, bool Propigate)
        {
            //Resize our incoming link and update
            List<clsSamplerLinkage> Links = new List<clsSamplerLinkage>(IncomingLinks);
            if (!Links.Contains(Link))
                Links.Add(Link);
            IncomingLinks = Links.ToArray();            
            SubItems[1].Text = IncomingLinks.Length.ToString();

            //Resize the outgoing link and update
            if (Propigate)
                Link.AddOutgoingLink(this, false);
        }

        /// <summary>
        /// Add in an outgoing link
        /// </summary>
        /// <param name="Link"></param>
        /// <param name="Propigate"></param>
        public void AddOutgoingLink(clsSamplerLinkage Link, bool Propigate)
        {
            List<clsSamplerLinkage> Links = new List<clsSamplerLinkage>(OutgoingLinks);
            if (!Links.Contains(Link))
                Links.Add(Link);
            OutgoingLinks= Links.ToArray();
            SubItems[2].Text = OutgoingLinks.Length.ToString();

            //Resize the incoming  link and update
            if (Propigate)
                Link.AddIncomingLink(this, false);
        }

        /// <summary>
        /// Add an incoming link
        /// </summary>
        /// <param name="Link"></param>
        /// <param name="Propigate"></param>
        public void RemoveIncomingLink(clsSamplerLinkage Link, bool Propigate)
        {
            //Resize our incoming link and update
            List<clsSamplerLinkage> Links = new List<clsSamplerLinkage>(IncomingLinks);
            Links.Remove(Link);
            IncomingLinks = Links.ToArray();
            SubItems[1].Text = IncomingLinks.Length.ToString();

            //Resize the outgoing link and update
            if (Propigate)
                Link.RemoveOutgoingLink(this, false);
        }

        /// <summary>
        /// Add in an outgoing link
        /// </summary>
        /// <param name="Link"></param>
        /// <param name="Propigate"></param>
        public void RemoveOutgoingLink(clsSamplerLinkage Link, bool Propigate)
        {
            List<clsSamplerLinkage> Links = new List<clsSamplerLinkage>(OutgoingLinks);
            Links.Remove(Link);
            OutgoingLinks = Links.ToArray();
            SubItems[2].Text = OutgoingLinks.Length.ToString();

            //Resize the incoming  link and update
            if (Propigate)
                Link.RemoveIncomingLink(this, false);
        }
        #endregion
    }
}